package com.winning.bi.etl;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.configuration.ConfigurationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;
import com.winning.bi.jdbc.dao.Dao;
import com.winning.bi.jdbc.dao.Page;

/**
 * 
 * @author 张大川
 *
 */
public class Etl {
	JTable jtable1;
	private JFrame frame;
	private JPanel panel;
	private JScrollPane scrollPane;
	final Object[] columnNames = { "任务ID", "任务名称", "进度", "源数据", "目标数据", "队列", "字段映射", "创建时间", "最后一次处理时间" };
	JMenuBar menuBar;
	JPanel jPanel1;

	private static ClassPathXmlApplicationContext ac;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Etl window = new Etl();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Etl() {
		initialize();
	}

	Object[][] tableData = null;
	DefaultTableModel tableModel;
	Page page1;

	Page page;
	String taskname;
	String source;
	String target;
	JTextField field;
	JTextField field2;
	JTextField field3;

	void query(Dao ao) {
		Map<String, String> map = new HashMap<String, String>();
		taskname = field.getText();
		source = field2.getText();
		target = field3.getText();
		map.put("taskname", taskname);
		map.put("source", source);
		map.put("target", target);
		page = new Page();
		page.setMap(map);
		try {
			page1 = ao.queryTaskInfo(page);
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(panel, "连接错误", "提示", JOptionPane.WARNING_MESSAGE);
			return;
		}
		tableData = page1.getStrings();
		tableModel.setDataVector(tableData, columnNames);
		tableModel.fireTableDataChanged();
	}

	
	private void initialize() {
		ac = new ClassPathXmlApplicationContext("file:config/applicationContext.xml");
		Dao dao = (Dao) ac.getBean("daoTask");
		page1 = new Page();
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension dim = kit.getScreenSize();
		field = new JTextField();
		field2 = new JTextField();
		field3 = new JTextField();
		Map<String, String> map = new HashMap<String, String>();

		page1.setMap(map);

		frame = new JFrame("ETL");
		double d = dim.getWidth() / 2;
		Double double1 = Double.valueOf(d);
		double e = dim.getHeight() / 2;
		Double double2 = Double.valueOf(e);
		frame.setBounds(100, 100, double1.intValue(), double2.intValue());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		menuBar = new JMenuBar();

		FormLayout formLayout = new FormLayout(
				new ColumnSpec[] { ColumnSpec.decode("20px"), ColumnSpec.decode("60px"),
						FormSpecs.UNRELATED_GAP_COLSPEC, ColumnSpec.decode("90px"), FormSpecs.UNRELATED_GAP_COLSPEC,
						ColumnSpec.decode("60px"), FormSpecs.UNRELATED_GAP_COLSPEC, ColumnSpec.decode("90px"),
						FormSpecs.UNRELATED_GAP_COLSPEC, ColumnSpec.decode("70px"), FormSpecs.UNRELATED_GAP_COLSPEC,
						ColumnSpec.decode("90px"), FormSpecs.UNRELATED_GAP_COLSPEC, ColumnSpec.decode("60px") },
				new RowSpec[] { RowSpec.decode("10px"), RowSpec.decode("20px") });
		panel = new JPanel(formLayout);

		panel.add(new JLabel("任务名称:"), "2, 2, left, center");
		panel.add(field, "4, 2, fill, top");
		panel.add(new JLabel("源数据表:"), "6, 2, left, center");
		panel.add(field2, "8, 2, fill, top");
		panel.add(new JLabel("目标数据表:"), "10, 2, left, center");
		panel.add(field3, "12, 2, fill, top");
		JButton button = new JButton("查询");
		JButton button5 = new JButton("下一页");
		JButton button3 = new JButton("上一页");
		button3.setEnabled(false);
		button5.setEnabled(false);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				query(dao);
				button3.setEnabled(true);
				button5.setEnabled(true);
			}
		});
		panel.add(button, "14, 2, fill, top");
		JMenu menu1 = new JMenu("系统");
		menuBar.add(menu1);
		JMenuItem item3 = new JMenuItem("源数据库");

		item3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {

					@Override
					public void run() {
						JFrame frame1 = null;
						try {
							frame1 = new SourceFrame();
						} catch (ConfigurationException e) {

							e.printStackTrace();
						}

						frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
						frame1.setVisible(true);

					}

				});
			}
		});
		menu1.add(item3);
		menu1.addSeparator();
		JMenuItem item4 = new JMenuItem("目标数据库");
		menu1.add(item4);
		menu1.addSeparator();

		item4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame1 = null;
				try {
					frame1 = new TargetFrame();
				} catch (ConfigurationException e1) {

					e1.printStackTrace();
				}

				frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				frame1.setVisible(true);

			}
		});

		JMenuItem item5 = new JMenuItem("任务数据库");
		menu1.add(item5);
		item5.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame1 = null;
				try {
					frame1 = new TaskFrame();
				} catch (ConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				frame1.setVisible(true);
			}
		});

		JPanel jPanel2 = new JPanel();

		jPanel1 = new JPanel();

		tableModel = new DefaultTableModel(tableData, columnNames);
		jtable1 = new JTable(tableModel);
		jtable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		scrollPane = new JScrollPane();
		scrollPane.setViewportView(jtable1);
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		frame.setJMenuBar(menuBar);

		JMenu jMenu = new JMenu("新建任务");
		jMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				NewTask frame1 = null;
				try {
					frame1 = new NewTask();
				} catch (ConfigurationException e1) {

					e1.printStackTrace();
				}
				frame1.setAc(ac);
				frame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				frame1.setVisible(true);

			}
		});
		JMenu jMenu2 = new JMenu("删除任务");
		jMenu2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				int i = jtable1.getSelectedRow();
				if (i < 0) {
					JOptionPane.showMessageDialog(frame.getContentPane(), "没有选中任何任务", "提示",
							JOptionPane.WARNING_MESSAGE);
					return;
				} else {
					Object[] strings = page1.getStrings()[i];

					int j = dao.deleteTask(strings[0].toString());
					JOptionPane.showMessageDialog(frame.getContentPane(), "删除" + j + "条数据", "提示",
							JOptionPane.WARNING_MESSAGE);

					query(dao);
				}
			}
		});

		JMenu jMenu3 = new JMenu("查看任务日志");
		jMenu3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int i = jtable1.getSelectedRow();

				if (i < 0) {
					JOptionPane.showMessageDialog(frame.getContentPane(), "没有选中任何任务", "提示",
							JOptionPane.WARNING_MESSAGE);
					return;
				} else {

					Object[] objects = tableData[i];
					System.out.println("jobid" + objects[0]);

					TaskLog frame = null;
					try {
						frame = new TaskLog(ac, objects[0].toString());
					} catch (Exception e1) {

						e1.printStackTrace();
					}

					frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
					frame.setVisible(true);

				}
			}
		});

		menuBar.add(jMenu);
		menuBar.add(jMenu2);
		menuBar.add(jMenu3);

		jPanel2.add(panel, BorderLayout.WEST);
		JPanel jPanel = new JPanel();

		button3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int i = page1.getCurrentPage();

				if (i - 1 > 0) {
					page1.setCurrentPage(i - 1);
					System.out.println("[上一页]" + (i - 1));
				} else {
					page1.setCurrentPage(1);
				}
				Map<String, String> map = new HashMap<String, String>();
				taskname = field.getText();
				source = field2.getText();
				target = field3.getText();
				map.put("taskname", taskname);
				map.put("source", source);
				map.put("target", target);
				page1.setMap(map);

				try {
					page1 = dao.queryTaskInfo(page1);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				tableData = page1.getStrings();
				tableModel.setDataVector(tableData, columnNames);
				tableModel.fireTableDataChanged();

			}
		});

		button5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				int i = page1.getCurrentPage();
				int j = i + 1;
				System.out.println("[下一页]" + j);
				page1.setCurrentPage(i + 1);
				Map<String, String> map = new HashMap<String, String>();
				taskname = field.getText();
				source = field2.getText();
				target = field3.getText();
				map.put("taskname", taskname);
				map.put("source", source);
				map.put("target", target);
				page1.setMap(map);
				try {
					page1 = dao.queryTaskInfo(page1);
				} catch (Exception e1) {

					e1.printStackTrace();
				}
				if (page1.getStrings().length < 1) {
					JOptionPane.showMessageDialog(frame.getContentPane(), "已经是最后一页", "提示", JOptionPane.WARNING_MESSAGE);
					page1.setCurrentPage(i);
					return;
				} else {
					tableData = page1.getStrings();
					tableModel.setDataVector(tableData, columnNames);
					tableModel.fireTableDataChanged();
				}

			}
		});

		jPanel.add(button3);
		jPanel.add(button5);

		jPanel2.setLayout(new BoxLayout(jPanel2, BoxLayout.X_AXIS));
		frame.getContentPane().add(jPanel2, BorderLayout.NORTH);
		frame.getContentPane().add(jPanel, BorderLayout.SOUTH);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
